#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <math.h>
#include <stdlib.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    currentThreadId=0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::burnFunction()
{
    for(int i=0;i<loopCount;i++)
    {
        double tmpa=rand();
        double tmpb=rand();
        atan2(tmpa,tmpb);
        sin(tmpa);
    }
    qDebug()<<"burnFunction!!!threadId="<<pthread_self()<<"loopCount="<<loopCount;
}


void* MainWindow::threadRun(void *)
{
     qDebug()<<"thread run!!!"<<pthread_self();
#if 0
    int max_cpus = sysconf(_SC_NPROCESSORS_ONLN);
    printf("Max CPUs = %d\n", max_cpus);

    /* lock all memory (prevent swapping) */
    if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
        printf("printf: mlockall\n");
    }

    set_latency_target();
    set_cpu(0);
#endif

    struct sched_param param;
    param.__sched_priority = priority;
    sched_setscheduler(0,SCHED_FIFO,&param);
    //setStackSize(81920);
//    int policy, priority;
//    pthread_getschedparam(pthread_self(), &policy, (struct sched_param *)&priority);
//    if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
//        qDebug("mlockall failed");
//    }
//    clock_gettime( CLOCK_MONOTONIC, &cycleTimespec );

   // qDebug()<<"this=="<<this;
   // pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); //允许退出线程
    //pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //设置立即取消

//    QThread::msleep(3000);

       // pthread_testcancel();/*the thread can be killed only here*/
//         QThread::msleep(1000);

        while(false==isQuitThread)
        {
//            QThread::msleep(D_SAFETY_LOOP_PERIOD);充电的时候没有响应
//            realTimeSleep(D_SAFETY_LOOP_PERIOD);
            // 循环安全检查
            burnFunction();
        }

    qDebug()<<"thread quit!!!"<<pthread_self();
//    printf("MainWindow thread quit!!!",pthread_self());
}

void* MainWindow::threadTask( void* classPtr )
{
    qDebug()<<" thread run-------------------------";
    return ((MainWindow*)classPtr)->threadRun(NULL);
}

void MainWindow::createThread()
{
    int ret=pthread_create( &threadId[currentThreadId], NULL, &MainWindow::threadTask,this);
    qDebug()<<" createThread-------------------------";
}
#define NSEC_PER_SEC 1000000000
void MainWindow::realTimeSleep(long long umicoroSecond)
{
    // 处理qt事件
//    QCoreApplication::processEvents();

    struct timespec time;
    clock_gettime(CLOCK_REALTIME,&time);
    time.tv_nsec += umicoroSecond * 1000;
    while(time.tv_nsec >= NSEC_PER_SEC)
    {
        time.tv_nsec -= NSEC_PER_SEC;
        ++time.tv_sec;
    }
    clock_nanosleep(CLOCK_REALTIME,TIMER_ABSTIME,&time,NULL);

}

void MainWindow::on_checkBox_isStart_clicked(bool checked)
{
    priority=ui->lineEdit_priority->text().toInt();
    threadCount=ui->lineEdit_thread_count->text().toInt();
    sleepTime=ui->lineEdit_sleep->text().toInt();
    loopCount=ui->lineEdit_loop_count->text().toInt();
    if(true==checked)
    {
        isQuitThread=false;
        for(int i=0;i<threadCount;i++)
        {
            createThread();
            currentThreadId++;
        }
    }
    else
    {
        isQuitThread=true;
    }

}
